package Initialize

import (
	"gitee.com/fierce_wolf/go-fox-edge-channel-common/channelInitialize"
	"gitee.com/fierce_wolf/go-fox-edge-common/commConfig"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEnv"
	"gitee.com/fierce_wolf/go-fox-edge-common/commLogger"
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedis"
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedisService"
	"go-fox-edge-channel-serial-analyzer/channelService"
	"go-fox-edge-channel-serial-analyzer/notify"
	"go-fox-edge-channel-serial-analyzer/streamEngine"
	"time"
)

func Initialize() {
	// 设置环境变量
	commEnv.Service.ServiceType = "service"
	commEnv.Service.ServiceName = "channel-serialport-analyzer-native"
	commEnv.Service.ModelType = "channel"
	commEnv.Service.ModelName = "serialport-analyzer"
	commEnv.Service.ApplicationName = "fox-edge-channel-serialport-analyzer-native"
	commEnv.Service.AppPort = 9201
	commEnv.SetWorkPath(2)

	// 连接redis
	commRedis.ConnectRedis()

	// 检查连接状态：连接不上就通过panic退出吧
	ok := commRedis.TestConnect()
	if !ok {
		panic("连接redis失败:" + commRedis.Options.Addr)
	}

	commLogger.Info("------------- channel-serial-analyzer-native 初始化开始！-------------")

	// 初始化服务
	channelService.BindChannelAPI()

	// 初始化公共模块
	consumer := make(map[string]interface{})
	consumer[commEntity.Type.OperateEntity.Type()] = nil
	channelInitialize.Initialize(consumer)

	// 绑定通知
	commRedisService.BindTypeNotify(commEntity.Type.OperateEntity.Type(), notify.Notify)

	// 初始化配置参数
	commConfig.InitializeByFilePath("serverConfig", "serverConfig.json")

	streamEngine.Initialize()

	commLogger.Info("------------- channel-serial-analyzer-native 初始化结束！-------------")
	commLogger.Info("------------- channel-serial-analyzer-native 驻留在后台！-------------")

	// 禁止退出
	for true {
		time.Sleep(1000 * time.Second)
	}
}
